<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>实时数据大屏</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/3.0.1/socket.io.js"></script>
    <style>
        body {
            margin: 0;
            padding: 20px;
            background-color: #1a1a1a;
            color: #fff;
            font-family: Arial, sans-serif;
        }
       .dashboard {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 20px;
            max-width: 1200px;
            margin: 0 auto;
        }
       .card {
            background-color: #2a2a2a;
            border-radius: 10px;
            padding: 20px;
            text-align: center;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
       .card h2 {
            margin: 0 0 10px 0;
            color: #4CAF50;
        }
       .value {
            font-size: 2.5em;
            font-weight: bold;
            margin: 10px 0;
        }
       .timestamp {
            text-align: right;
            color: #888;
            margin-top: 20px;
        }
        @keyframes pulse {
            0% { transform: scale(1); }
            50% { transform: scale(1.05); }
            100% { transform: scale(1); }
        }
       .update {
            animation: pulse 0.5s ease-in-out;
        }
    </style>
</head>
<body>
    <h1 style="text-align: center; margin-bottom: 40px;">实时数据监控</h1>
    <div class="dashboard">
        <div class="card">
            <h2>销售额</h2>
            <div id="sales" class="value">0</div>
            <div>实时销售金额 (元)</div>
        </div>
        <div class="card">
            <h2>访问量</h2>
            <div id="visitors" class="value">0</div>
            <div>当前访问人数</div>
        </div>
        <div class="card">
            <h2>订单数</h2>
            <div id="orders" class="value">0</div>
            <div>实时订单统计</div>
        </div>
    </div>
    <div class="timestamp" id="timestamp">最后更新时间: --:--:--</div>

    <script>
        const socket = io();

        // 更新数据的函数
        function updateValue(elementId, value) {
            const element = document.getElementById(elementId);
            element.textContent = value;
            element.classList.remove('update');
            void element.offsetWidth; // 触发重绘
            element.classList.add('update');
        }

        // 监听数据更新事件
        socket.on('update_data', function(data) {
            console.log(data); // 打印接收到的数据
            updateValue('sales', data.sales);
            updateValue('visitors', data.visitors);
            updateValue('orders', data.orders);
            document.getElementById('timestamp').textContent = '最后更新时间: ' + data.timestamp;
        });
    </script>
</body>
</html>